//
//  SDSelectDiseaseView.m
//  SuperDoctor
//
//  Created by 李忠 on 7/8/16.
//  Copyright © 2016 SD. All rights reserved.
//

#import "SDFakeButton.h"
#import "SDFamousDoctorsView.h"
#import <objc/message.h>

#define kPadding (10*_sdScale)
// 运行时objc_msgSend
#define SDMsgSend(...) ((void (*)(void *, SEL, UIView *))objc_msgSend)(__VA_ARGS__)
#define SDTarget(target) (__bridge void *)(target)


@interface SDFakeButton ()

@property (nonatomic, strong) UILabel *lb_diseaseType;

//默认类型下，显示图片
@property (nonatomic, strong) UIImageView *iv_defalutImage;

//SDFakeButtonSelected 类型下显示的空间
@property (nonatomic, strong) UIButton *btn_first;
@property (nonatomic, strong) UIButton *btn_last;
@property (nonatomic, assign) SDFakeButtonType buttonType;


//记录被选中的按钮
@property (nonatomic, weak) UIButton *selectedBtn;


@property(nonatomic,strong) id target;


@property(nonatomic) SEL action;

@property (nonatomic, assign) CGFloat sdScale;

@end

@implementation SDFakeButton

- (instancetype)initWithType:(SDFakeButtonType)type{
    self = [super init];
    [self checkDevice];
    _buttonType = type;
    self.backgroundColor = [UIColor whiteColor];
    if (self) {
        
        switch (type) {
            case SDFakeButtondefault:
                
                [self setupDefaultLayout];
                break;
                
            case SDFakeButtonSelected:
                
                [self setupSelectedLayout];
                [self setupAddtarget];
                break;
        }
        
        
    }
    return self;
}

- (void)checkDevice{
    if (kDevice_Is_iPhone5) {
        _sdScale = 0.8;
    }else if (kDevice_Is_iPhone6){
        _sdScale = 1;
    }else if (kDevice_Is_iPhone6Plus){
        _sdScale = 1;
    }else{
        _sdScale = 0.7;
    }
}


#pragma mark
#pragma mark - 设置SDFakeButtondefault 状态下的图片
- (void)setupDefaultLayout{
    
    UIImageView *separate = [[UIImageView alloc] init];
    
    separate.image = [UIImage imageNamed:@"fengexian"];
    [self addSubview:separate];
    
    [self.lb_diseaseType mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerY.mas_equalTo(self.mas_centerY);
        make.leading.mas_equalTo(kPadding);

    }];
    [separate mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.mas_equalTo(self.lb_diseaseType.mas_trailing);
        make.top.mas_equalTo(kPadding * 0.5);
        make.bottom.mas_equalTo(-kPadding * 0.5);
        make.width.mas_equalTo(2);
    }];
    [self.iv_defalutImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.mas_equalTo(separate.mas_trailing).mas_offset(2*kPadding);
        make.trailing.mas_equalTo(-2 * kPadding);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(15,15));
        
    }];
    
}

#pragma mark
#pragma mark - 设置SDFakeButtonSelected状态下的图片
- (void)setupSelectedLayout{
    
    [self.lb_diseaseType mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.mas_centerY);
        make.leading.mas_equalTo(kPadding);
        make.trailing.mas_equalTo(self.mas_centerX);
    }];
    [self.btn_first mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.mas_equalTo(self.mas_centerX);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.height.width.mas_equalTo(self.btn_last);
    }];
    [self.btn_last mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.mas_equalTo(self.btn_first.mas_trailing);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.trailing.mas_equalTo(self.mas_trailing);
        
    }];
}

#pragma mark
#pragma mark - 设置两个按钮的switch方法
- (void)setupAddtarget{
    
    
    [self.btn_last addTarget:self action:@selector(clicSwtichButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.btn_first addTarget:self action:@selector(clicSwtichButton:) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark
#pragma mark - 点击按钮的实现方法
- (void)clicSwtichButton:(UIButton *)sender{
    
    self.lb_diseaseType.textColor = [UIColor blackColor];
    self.selectedBtn.selected = NO;
    self.selectedBtn.userInteractionEnabled = YES;
    self.selectedBtn = sender;
    self.selectedBtn.userInteractionEnabled = NO;
    self.selectedBtn.selected = YES;
    NSInteger index = [self.subviews indexOfObject:sender];
    
    if (index == 1) {
        self.lb_diseaseType.text = @"已确认";
    }else{
        self.lb_diseaseType.text = @"症状疑似";
    }
    if (index == 1) {
        if (_hiddenFateBlock) {
            _hiddenFateBlock(NO);
            self.lb_diseaseType.text = @"接受过";
        }
        
    }else if (index == 2){
        if (_hiddenFateBlock) {
            _hiddenFateBlock(YES);
             self.lb_diseaseType.text = @"未接受过";
        }
        
    }
    
    
    
}

#pragma mark
#pragma mark - 实现方法

//设置提示标签的文字颜色
- (void)setNoticColor:(UIColor *)color{
    self.lb_diseaseType.textColor = color;
}
//设置天使文本的样式与样式
- (void)setNoticeTitle:(NSString *)title withColor:(UIColor *)color{
    self.lb_diseaseType.text = title;
    [self setNoticColor:color];
}

//根据当前不同的状态，设置不同的图片，如果是SDFakeButtonSelected 样式，就是设置UIControlStateNormal图片
- (void)setImage:(UIImage *)image{
    if (_buttonType == SDFakeButtondefault) {
        self.iv_defalutImage.image = image;
    }else{
        [self.btn_first setImage:image forState:UIControlStateNormal];
        [self.btn_last setImage:image forState:UIControlStateNormal];
    }
    
}
//设置SDFakeButtonSelected 样式下的按钮图片 设置UIControlStateNormal状态
- (void)setSelectImage:(UIImage *)image{
    
    if (_buttonType == SDFakeButtondefault)return;

    [self.btn_first setImage:image forState:UIControlStateSelected];
    [self.btn_first setImage:image forState:UIControlStateSelected];
}

//设置switch 按钮的在某个状态下显示的文本
- (void)setFirstTitle:(NSString *)firstTitle withSecondTitle:(NSString *)secondTItle forState:(UIControlState)state{

    //判断当前按钮是否样式下的
    if (_buttonType == SDFakeButtondefault) return;
    [self.btn_first setTitle:firstTitle forState:state];
    [self.btn_last setTitle:secondTItle forState:state];
}

- (void)setNorImage:(UIImage *)norImage withSelectedImage:(UIImage *)selectedImage{
    //判断当前按钮是否样式下的
    if (_buttonType == SDFakeButtondefault) return;
    [self.btn_first setImage:norImage forState:UIControlStateNormal];
    [self.btn_first setImage:selectedImage forState:UIControlStateSelected];
    
    [self.btn_last setImage:norImage forState:UIControlStateNormal];
    [self.btn_last setImage:selectedImage forState:UIControlStateSelected];
}

- (void)setSwitchBtnTitleColor:(UIColor *)color forState:(UIControlState)state{
    //判断当前按钮是否样式下的
    if (_buttonType == SDFakeButtondefault) return;
    
    [self.btn_first setTitleColor:color forState:state];
    [self.btn_last setTitleColor:color forState:state];
}

//设置默认选中的按钮
- (void)setDefaultButton:(BOOL)flag{
    //YES 是第一个，NO是第二个
    self.selectedBtn.selected = NO;
    if (flag) {
        self.selectedBtn = self.btn_first;
        self.btn_first.selected = YES;
        self.btn_first.userInteractionEnabled = NO;
    }else{
        self.selectedBtn = self.btn_last;
        self.btn_last.selected = YES;
        self.btn_last.userInteractionEnabled = NO;
    }
}


#pragma mark
#pragma mark - 实现点击方法


- (void)addTarget:(id)target action:(SEL)action{

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickFate11:)];

    self.target = target;
    self.action = action;
    
    [self addGestureRecognizer:tap];
    
}


#pragma mark - dianji
- (void)clickFate11:(UITapGestureRecognizer *)obj{

    
//    [self.target performSelector:self.action withObject:self];

    SDMsgSend(SDTarget(self.target),self.action,self);
    

    

    
}

#pragma mark
#pragma mark - lazy 懒加载控件
//MARK: SDFakeButtonDefault 和 SDFakeButtonSelected 状态下都有的空间
- (UILabel *)lb_diseaseType{
    if (_lb_diseaseType == nil) {
        _lb_diseaseType = [[UILabel alloc] init];
        _lb_diseaseType.textColor = [UIColor lightGrayColor];
        
        [self addSubview:_lb_diseaseType];
    }
    return _lb_diseaseType;
}
//MARK: SDFakeButtonDefault 状态下的控件
- (UIImageView *)iv_defalutImage{
    if (_iv_defalutImage == nil) {
        _iv_defalutImage = [[UIImageView alloc] init];
        [self addSubview:_iv_defalutImage];
    }
    return _iv_defalutImage;
}

//MARK: SDFakeButtonSelected 状态下的按钮lazy
- (UIButton *)btn_first{
    if (_btn_first == nil) {
        
        _btn_first  = [self newButton];
        _btn_first.titleLabel.font = [UIFont systemFontOfSize:17*_sdScale];
        [self addSubview:_btn_first];
    }
    return _btn_first;
}
- (UIButton *)btn_last{
    if (_btn_last == nil) {
        
        _btn_last = [self newButton];
        _btn_last.titleLabel.font = [UIFont systemFontOfSize:17*_sdScale];
        [self addSubview:_btn_last];
    }
    return _btn_last;
}

- (UIButton *)newButton{
    
    UIButton *btn = [[UIButton alloc] init];
//    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    btn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
    
    return btn;
}



@end
